package com.ctg.payroll2.dao.social.impl;

import java.sql.SQLException;
import java.util.List;

import com.ctg.payroll2.bean.filter.social.CsEmplsocialFilter;
import com.ctg.payroll2.bean.filter.social.PbEmployeeFilter;
import com.ctg.payroll2.bean.po.social.CsEmplsocial;
import com.ctg.payroll2.dao.social.EmployeeSocialRecordDao;
import com.ctg.platform.orm.ibatis.IbatisEntityDao;
import com.ctg.platform.util.Page;
/**
 * 雇员社保档案Dao实现
 * @author 鲁力 2009-9-11 上午11:58:27
 *
 */
@SuppressWarnings("unchecked")
public class EmployeeSocialRecordDaoImpl extends IbatisEntityDao implements
		EmployeeSocialRecordDao {
	private String getNameSpace(){
		return getClass().getSimpleName();
	}

	public Page queryCsEmplsocial(CsEmplsocialFilter filter) {
		return pagedQueryByFilter(this.getClass(), filter,
				".queryCsEmplsocial_count",
				".queryCsEmplsocial_query");
	}
	
	public Page queryCsEmployee(PbEmployeeFilter filter) {
		return pagedQueryByFilter(this.getClass(), filter,
				".queryCsEmployee_count", ".queryCsEmployee_query");
	}
	public Page queryPbSocialItemsResult(CsEmplsocialFilter filter){
		return pagedQueryByFilter(this.getClass(), filter,
				".queryPbSocialItemsResult_count",
				".queryPbSocialItemsResult_query");
	}
	public Page queryRecomSocialTemplate(CsEmplsocialFilter filter) {
		return pagedQueryByFilter(this.getClass(), filter,
				".queryRecomSocialTemplate_count",
				".queryRecomSocialTemplate_query");
	}
	public List queryRecomSocialTemplateDetail(CsEmplsocialFilter filter) {
		return getSqlMapClientTemplate().queryForList(getNameSpace() + ".queryRecomSocialTemplateDetail", filter);
	}
	
	public Page queryCsSocialTemplate(CsEmplsocialFilter filter){
		return pagedQueryByFilter(this.getClass(), filter,
				".queryCsSocialTemplate_count",
				".queryCsSocialTemplate_query");
	}
	
	public List queryCsSocialTemplateDetail(CsEmplsocialFilter filter){
		return getSqlMapClientTemplate().queryForList(getNameSpace() + ".queryCsSocialTemplateDetail", filter);
	}
	
	/**
	 * 员工社保档案详细信息翻页查询
	 * @param filter
	 * @return
	 */
	public Page querycsEmplsocialDetails(CsEmplsocialFilter filter) {
		return pagedQueryByFilter(this.getClass(), filter,
				".querycsEmplsocialDetails_count",
				".querycsEmplsocialDetails_query");
	}
	public CsEmplsocial retrieveEmployeeByFilter(CsEmplsocialFilter filter) throws SQLException {
		return (CsEmplsocial) getSqlMapClient().queryForObject(getNameSpace() + ".retrieveEmployeeByFilter", filter);
	}
	
	public void insertCsEmplsocial(CsEmplsocial csEmplsocial) throws SQLException{
		if (csEmplsocial.getSocial_security_id() == null) {
			logger.fatal("<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<social_security_id 不能为空!>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
			return;
		}
		getSqlMapClient().insert(getNameSpace() + ".insertCsEmplsocial", csEmplsocial);
	}
	
	public int checkisNeedToDemolish(CsEmplsocial emplsocial) throws SQLException{
		return (Integer)getSqlMapClient().queryForObject(getNameSpace() + ".checkisNeedToDemolish", emplsocial);
	}
	
	public void fakeDeleteCsEmplsocial(String ids){
		getSqlMapClientTemplate().update(getNameSpace() + ".fakeDeleteCsEmplsocial", ids);
	}
	
	public void demolishCsEmpSocial(CsEmplsocial emplsocial) throws SQLException{
		if (emplsocial.getSocial_security_id() == null) {
			logger.fatal("<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<social_security_id 不能为空!>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
			return;
		}
		getSqlMapClient().update(getNameSpace() + ".demolishCsEmpSocialFirstStep", emplsocial);
		getSqlMapClient().insert(getNameSpace() + ".demolishCsEmpSocialSecondStep", emplsocial);
	}
	public int updateCsEmpSocial(CsEmplsocial emplsocial) throws SQLException{
		getSqlMapClient().update(getNameSpace() + ".updateCsEmpSocial", emplsocial);
		return 0;
	}
	public List getEmployeeBySocialItem(CsEmplsocialFilter filter) throws SQLException{
		return getSqlMapClient().queryForList(getNameSpace() + ".getEmployeeBySocialItem", filter);
	}

	public String checkEmpSSocialItemIsCaled(CsEmplsocial csEmplsocial) throws SQLException {
		List<CsEmplsocial> temp = getSqlMapClient().queryForList(getNameSpace() + ".checkEmpSSocialItemIsCaled", csEmplsocial);
		StringBuffer sb = new StringBuffer();
		for (CsEmplsocial social : temp) {
			if (Integer.parseInt(social.getCount())>0) {
				sb.append(social.getShow_name()).append(",");
			}
		}
		return sb.length()==0?sb.toString():sb.append("这几个社保项目如果不计入结果，之前计算过的内容不会被重算，如果需要重算，进行时间段分拆，请确认！").toString();
	}
	
	public int bjrjg(CsEmplsocial csEmplsocial) throws SQLException {
		return getSqlMapClient().update(getNameSpace() + ".bjrjg1", csEmplsocial)+getSqlMapClient().update(getNameSpace() + ".bjrjg2", csEmplsocial);
	}
}
